/** 
 * This class provides formatting functions that allow programmers to
 * convert an integer or floating point number into a string with a
 * specified, width and precision. and format. The class also formats
 * a string to a specified width, aligned left or right, filled out to
 * the specified width with spaces. <P>
 *
 * There are a number of possible things one can do when a number will
 * not fit inside the specified format. You can throw an exception,
 * truncate the number, return an error string, or expand the width.
 * Here I've chosen to expand the width. <P>
 *
 * The rounding of these precisions still needs work. Currently excess
 * digits are merely truncated.<P>
 *
 * @Version: 0.2 of June 10, 2001
 * @Author: J. Santiago Jorge (sjorge@dc.fi.udc.es)
 */


public class Format {

    /** 
     *
     * This method formats an integer to a specified width,
     * no leading zeroes,
     * right aligned, without +
     * signs on positive numbers, filled out to the specified width
     * with spaces
     *
     * @param d The number to be formatted
     * @param width The minimum number of characters in the returned string
     * @return A String containing a formatted representation of the number
     */
    public static String format(long d, int width) {
        java.lang.String s = java.lang.Long.toString(d);
        java.lang.String str = new java.lang.String();
        for(int i=0; i < width - s.length(); i++)
            str += " ";
        return(str + s);
    }

    /** 
     *
     * This method formats a floating point number to specified width,
     * number of decimal places, in decimal format,
     * right aligned, without +
     * signs on positive numbers. Spaces 
     * fill out the number to the specified width.
     *
     * @param x The number to be formatted
     * @param width The minimum number of characters in the returned string
     * @param precision The number of digits after the decimal point
     * @return A String containing a formatted representation of the number.
     */
    public static String format(double x, int width, int precision) {
        java.lang.StringBuffer strBuf = new java.lang.StringBuffer();
        java.lang.String str = new java.lang.String();

        java.text.DecimalFormat decForm =
            (java.text.DecimalFormat) java.text.NumberFormat.getInstance(java.util.Locale.ITALIAN);
        
        java.text.FieldPosition intPos =
            new java.text.FieldPosition(java.text.DecimalFormat.INTEGER_FIELD);

        decForm.setMinimumFractionDigits(precision);
        decForm.setMaximumFractionDigits(precision);

        decForm.format(x, strBuf, intPos);

        for(int i=0; i < width - strBuf.length(); i++)
            str += " ";

        return(str + strBuf);
    }

    /**
     * This method formats a string to a specified width,
     * aligned left or right,
     * filled out to the specified width with spaces
     *
     * @param s The string to be formatted
     * @param width The minimum number of characteres in the returned
     * string
     * @param alignLeft True for left alignment in the width, 
     * false for right alignment
     * @return A String containing a formatted representation of the
     * string
     */
    public static String format(String s, int width, boolean alignLeft) {
        java.lang.String str = new java.lang.String();
        for(int i=0; i < width - s.length(); i++)
            str += " ";
        if (alignLeft)
            return(s + str);
        else
            return(str + s);
    }
}